import { find } from 'lodash-es'

function normalizeQueryOptions(model) {
  const {
    columns = [],
    queryOptions = {}
  } = model || {}
  const normalized = []

  Object.keys(queryOptions).forEach(key => {
    const option = queryOptions[key]
    const {
      title,
      ...columnProps
    } = find(columns, ['key', key]) || {}
    const label = option.title || title || key

    if (label) {
      normalized.push({
        label,
        key,
        ...columnProps,
        ...option
      })
    }
  })

  return normalized
}

export default normalizeQueryOptions
